import logging
logging.basicConfig(filename='/tmp/iswm.log', level=logging.DEBUG)
import weakref

from base import Object

class Client(Object):
    all_clients = []
    window_to_client_map = weakref.WeakValueDictionary()
    
    @classmethod
    def get_by_window(cls, window):
        return cls.window_to_client_map.get(window)
        
        
    def __init__(self, screen, window, geom):
        Object.__init__(self)
        
        self.screen = screen
        self.geom = geom
        self.window = window
        self.conn = window.conn
        self.core = screen.core
        
        self.all_clients.append(self)
        self.window_to_client_map[self.window] = self
        
        #self.apply_normal_hints()
        self.conn.flush()
    
    def unmanage(self):
        self.window.change_attributes(event_mask=0)
        try:
            self.all_clients.remove(self)
            del self.window_to_client_map[self.window]
        except (ValueError, KeyError), e:
                logging.warning(e)
        
        self.window.remove_handlers(self)
    
    def focus(self):
        self.window.set_input_focus()
        self.conn.flush()
    
    def resize(self, geom):
        self.window.configure_checked(width=geom.width, height=geom.height).check()
        self.conn.flush()
    
    def update_geom(self, geom):
        self.geom = geom
    
    #def apply_normal_hints(self, hints=None, geom=None):
